define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/index',
                    table: 'order',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-show").data("area", ['90%', '90%']);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                pageSize: 100,
                queryParams: function (params) {
                    params.limit = 100;
                    return params;
                },
                columns: [
                    [
                        {field: 'day', title: __('时间')},
                        {field: 'user', title: __('新增会员数'), formatter: Controller.api.formatter.user},
                        {field: 'box', title: __('送出盒子数'), formatter: Controller.api.formatter.box},
                        {field: 'order', title: __('订单数'), formatter: Controller.api.formatter.order},
                        {field: 'order_total', title: __('订单金额'), formatter: Controller.api.formatter.order_total},
                        {field: 'pv', title: __('PV'), formatter: Controller.api.formatter.pv},
                        {field: 'uv', title: __('UV'), formatter: Controller.api.formatter.uv},
                        {field: 'damage', title: __('货损'), formatter: Controller.api.formatter.damage},
                    ]
                ]
            });

            $(document).on("click", ".btn-show", function (e) {
                var url = $(this).data("url");
                var start = $(this).data("start");
                var end = $(this).data("end");
                var title = $(this).data("title");
                var day = $(this).data("day");
                Backend.api.open(url + '?start=' + start + '&end=' + end, __(day + title), {

                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        user: function () {
            var table = $("#table");
            var start = table.attr('start');
            var end = table.attr('end');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/user?start=' + start + '&end=' + end,
                    table: 'users',
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_id',
                sortName: 'user_id',
                commonSearch: false,
                searchFormVisible: false,
                columns: [
                    [
                        {field: 'user_id', title: __('用户ID'), operate: false},
                        {field: 'phone', title: __('注册手机号'), operate: false},
                        {field: 'nick_name', title: __('昵称'), operate: false},
                        {field: 'createtime', title: __('注册时间'), operate: false},
                    ]
                ]
            });
            Controller.api.bindevent();
        },
        box: function () {
            var table = $("#table");
            var start = table.attr('start');
            var end = table.attr('end');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/box?start=' + start + '&end=' + end,
                    table: 'user_box',
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_box_id',
                sortName: 'user_box_id',
                commonSearch: false,
                searchFormVisible: false,
                columns: [
                    [
                        {field: 'box_no', title: __('盒子编号'), operate: false},
                        {field: 'user_id', title: __('所属用户ID'), operate: false},
                        {field: 'area', title: __('所属区域'), formatter: Controller.api.formatter.city_info, operate: false},
                        {field: 'room', title: __('所属小区'), formatter: Controller.api.formatter.house_info, operate: false},
                        {field: 'contacts', title: __('联系人'), operate: false},
                        {field: 'telephone', title: __('联系电话'), operate: false},
                        {field: 'place_time_start', title: __('布放时间'), operate: false},
                    ]
                ]
            });
            Controller.api.bindevent();
        }
        ,
        order: function () {
            var table = $("#table");
            var start = table.attr('start');
            var end = table.attr('end');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/order?start=' + start + '&end=' + end,
                    table: 'order',
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                commonSearch: false,
                searchFormVisible: false,
                columns: [
                    [
                        {field: 'order_no', title: __('订单号'), operate: false},

                        {field: 'nick_name', title: __('用户昵称'), operate: false},
                        {field: 'phone', title: __('注册手机号'), operate: false},
                        {field: 'address', title: __('居住地址'), formatter: Controller.api.formatter.address, operate: false},
                        {field: 'goods_price', title: __('商品金额'), operate: false},
                        {field: 'pay_price', title: __('实付金额'), operate: false},
                        {field: 'pay_type_text', title: __('支付方式'), operate: false},
                        {field: 'status_text', title: __('订单状态'), operate: false},
                        {field: 'createtime', title: __('下单时间'), operate: false},
                    ]
                ]
            });
            Controller.api.bindevent();
        }
        ,
        pv: function () {
            var table = $("#table");
            var start = table.attr('start');
            var end = table.attr('end');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/pv?start=' + start + '&end=' + end,
                    table: 'pv',
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'pv_id',
                sortName: 'pv_id',
                commonSearch: false,
                searchFormVisible: false,
                columns: [
                    [
                        {field: 'user_id', title: __('用户ID'), operate: false},
                        {field: 'phone', title: __('注册手机号'), operate: false},
                        {field: 'nick_name', title: __('昵称'), operate: false},
                        {field: 'createtime', title: __('访问时间'), operate: false},
                    ]
                ]
            });
            Controller.api.bindevent();
        }
        ,
        uv: function () {
            var table = $("#table");
            var start = table.attr('start');
            var end = table.attr('end');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/uv?start=' + start + '&end=' + end,
                    table: 'uv',
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'uv_id',
                sortName: 'uv_id',
                commonSearch: false,
                searchFormVisible: false,
                columns: [
                    [
                        {field: 'user_id', title: __('用户ID'), operate: false},
                        {field: 'phone', title: __('注册手机号'), operate: false},
                        {field: 'nick_name', title: __('昵称'), operate: false},
                        {field: 'createtime', title: __('访问时间'), operate: false},
                    ]
                ]
            });
            Controller.api.bindevent();
        }
        ,
        damage: function () {
            var table = $("#table");
            var start = table.attr('start');
            var end = table.attr('end');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/damage?start=' + start + '&end=' + end,
                    table: 'damage',
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'damage_id',
                sortName: 'damage_id',
                commonSearch: false,
                searchFormVisible: false,
                columns: [
                    [
                        {field: 'box_no', title: __('盒子编号'), operate: false},
                        {field: 'user_id', title: __('所属用户ID'), operate: false},
                        {field: 'area', title: __('所属区域'), formatter: Controller.api.formatter.city_info, operate: false},
                        {field: 'room', title: __('所属小区'), formatter: Controller.api.formatter.house_info, operate: false},
                        {field: 'contacts', title: __('联系人'), operate: false},
                        {field: 'telephone', title: __('联系电话'), operate: false},
                        {field: 'damage_amount', title: __('本次货损'), operate: false},
                        {field: 'sum_amount', title: __('总货损'), operate: false},
                        {field: 'createtime', title: __('核查时间'), operate: false},
                        {field: 'damage_level', title: __('货损级别'), operate: false},
                    ]
                ]
            });
            Controller.api.bindevent();
        }
        ,
        damage: function () {
            var table = $("#table");
            var start = table.attr('start');
            var end = table.attr('end');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/damage?start=' + start + '&end=' + end,
                    table: 'damage',
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'damage_id',
                sortName: 'damage_id',
                commonSearch: false,
                searchFormVisible: false,
                columns: [
                    [
                        {field: 'box_no', title: __('盒子编号'), operate: false},
                        {field: 'user_id', title: __('所属用户ID'), operate: false},
                        {field: 'area', title: __('所属区域'), formatter: Controller.api.formatter.city_info, operate: false},
                        {field: 'room', title: __('所属小区'), formatter: Controller.api.formatter.house_info, operate: false},
                        {field: 'contacts', title: __('联系人'), operate: false},
                        {field: 'telephone', title: __('联系电话'), operate: false},
                        {field: 'damage_amount', title: __('本次货损'), operate: false},
                        {field: 'sum_amount', title: __('总货损'), operate: false},
                        {field: 'createtime', title: __('核查时间'), operate: false},
                        {field: 'damage_level', title: __('货损级别'), operate: false},
                    ]
                ]
            });
            Controller.api.bindevent();
        },
        recharge: function () {
            var table = $("#table");
         
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/recharge',
                    table: 'order',
                }
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                columns: [
                    [
                        {field: 'user_id', title: __('用户ID')},
                        {field: 'order_no', title: __('充值订单号')},
                        {field: 'real_price', title: __('充值金额'), operate: false},
                        {field: 'give_price', title: __('赠送金额'), operate: false},
                        {field: 'pay_at', title: __('充值时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }, formatter: {//渲染的方法
                user: function (value, row, index) {
                    var html = '<a class="text-success btn-show"  style="cursor:pointer" data-title="新增会员数" data-url="report/user" data-day="' + row.day + '" data-start="' + row.start + '" data-end="' + row.end + '">' + row.user + '</a>';
                    return html;
                }
                ,
                box: function (value, row, index) {
                    var html = '<a class="text-success btn-show"  style="cursor:pointer" data-title="送出盒子数" data-url="report/box" data-day="' + row.day + '" data-start="' + row.start + '" data-end="' + row.end + '">' + row.box + '</a>';
                    return html;
                }
                ,
                order: function (value, row, index) {
                    var html = '<a class="text-success btn-show"  style="cursor:pointer" data-title="订单数" data-url="report/order" data-day="' + row.day + '" data-start="' + row.start + '" data-end="' + row.end + '">' + row.order + '</a>';
                    return html;
                }
                ,
                order_total: function (value, row, index) {
                    var html = '<a class="text-success btn-show"  style="cursor:pointer" data-title="订单金额" data-url="report/order" data-day="' + row.day + '" data-start="' + row.start + '" data-end="' + row.end + '">' + row.order_total + '</a>';
                    return html;
                }
                ,
                pv: function (value, row, index) {
                    var html = '<a class="text-success btn-show"  style="cursor:pointer" data-title="PV" data-url="report/pv" data-day="' + row.day + '" data-start="' + row.start + '" data-end="' + row.end + '">' + row.pv + '</a>';
                    return html;
                }
                ,
                uv: function (value, row, index) {
                    var html = '<a class="text-success btn-show"  style="cursor:pointer" data-title="UV" data-url="report/uv" data-day="' + row.day + '" data-start="' + row.start + '" data-end="' + row.end + '">' + row.uv + '</a>';
                    return html;
                }
                ,
                damage: function (value, row, index) {
                    var html = '<a class="text-success btn-show"  style="cursor:pointer" data-title="货损" data-url="report/damage" data-day="' + row.day + '" data-start="' + row.start + '" data-end="' + row.end + '">' + row.damage + '</a>';
                    return html;
                },
                city_info: function (value, row, index) {
                    var html = row.province_name + '-' + row.city_name + '-' + row.area_name;
                    return html;
                },
                house_info: function (value, row, index) {

                    var html = row.quarters_name + '-' + row.unit_name + '-' + row.room_name;
                    return html;
                },
                address: function (value, row, index) {
                    var html = '-';
                    if (row.province_name) {
                        html = row.province_name + '-' + row.city_name + '-' + row.area_name + "<br>";
                        html += row.quarters_name + '-' + row.unit_name + '-' + row.room_name;
                    }

                    return html;
                },
            },
        }
    };
    return Controller;
});